package org.example.service.imp;

import lombok.extern.slf4j.Slf4j;
import org.example.dao.imp.StudentDaoImp;
import org.example.dao.imp.UserDaoImp;
import org.example.entitys.managers.Student;
import org.example.entitys.systems.User;
import org.example.service.inter.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/13
 */

@Service
@Slf4j
public class StudentServiceImp implements StudentService {
    @Autowired
    StudentDaoImp studentDaoImp;
    @Autowired
    UserDaoImp userDaoImp;

    @Override
    public boolean addStudent(Student student) {
        if (studentDaoImp.isExitById(student.getId())) return false;//判断学生是否存在
        if (student.getLogin_id() == -1 || !userDaoImp.isExitById(student.getLogin_id())) {
            //判断用户是否存在，不存在就添加用户。
            User user = new User(student);//通过student创建user类
            userDaoImp.insert(user);//插入用户
            student.setLogin_id(userDaoImp.queryLastInsertID());//获取刚插入用户数据
            userDaoImp.insertIdentify(student.getLogin_id(), userDaoImp.queryRoleIdByName("student"));//添加user与role的关系
        }
        return studentDaoImp.insert(student) == 1;//判断插入是否正确
    }
//
//    @Override
//    public boolean updateStudent(Student student) {
//        if (!studentDaoImp.isExitById(student.getId()) || student.getId() == -1) return false;
//        return studentDaoImp.update(student) == 1;
//    }
//
//    @Override
//    public boolean deleteStudent(int id) {
//        return studentDaoImp.delete(id) == 1;
//    }

    @Override
    public boolean inputStudents(LinkedList<Student> students) {
        for(Student student:students)
            if (!addStudent(student))
                log.error(student+"导入错误！！！");
//        System.out.println(students);
        return true;
    }

    @Override
    public boolean checkStudent(int id) {//审核学生
        if (!studentDaoImp.isExitById(id)) return false;
        return studentDaoImp.checkStudent(id) == 1;
    }

    @Override
    public boolean checkStudents(int[] ids) {
        for (int id : ids) {
            if (studentDaoImp.isExitById(id)) {
                studentDaoImp.checkStudent(id);
            }
        }
        return true;
    }
//
//    @Override
//    public Student[] getAllReviewedStudents() {
//        return studentDaoImp.queryAllReviewedStudents().toArray(new Student[0]);
//    }
//
    @Override
    public List<Student> getAllStudents() {
        return studentDaoImp.queryAll();
    }
}
